﻿#region COPYRIGHT
//--------------------------------------------------------------------------------
// <copyright file="Lifetime.cs" company="starLiGHT Entertainment Studios">
//    Copyright (c) 2008, 2009, 2010, 2011
//       Roland Rosenkranz (Glatzemann@email.de)
// </copyright>
// <license>
//   This file is part of starLiGHT.Particles.
//
//   starLiGHT.Particles is free software: you can redistribute it and/or modify
//   it under the terms of the GNU Lesser General Public License as published by
//   the Free Software Foundation, either version 3 of the License, or
//   (at your option) any later version.
//
//   starLiGHT.Particles is distributed in the hope that it will be useful,
//   but WITHOUT ANY WARRANTY; without even the implied warranty of
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//   GNU Lesser General Public License for more details.
//
//   You should have received a copy of the GNU Lesser General Public License
//   along with starLiGHT.Particles. If not, see http://www.gnu.org/licenses/.
//    
//   ADDITIONAL (commercial) LICENSES for starLiGHT.Particles are available on request.
// </license>
// <original_license>
//  * FLINT PARTICLE SYSTEM
//  * .....................
//  * 
//  * Author: Richard Lord
//  * Copyright (c) Richard Lord 2008-2009
//  * http://flintparticles.org
//  * 
//  * 
//  * Licence Agreement
//  * 
//  * Permission is hereby granted, free of charge, to any person obtaining a copy
//  * of this software and associated documentation files (the "Software"), to deal
//  * in the Software without restriction, including without limitation the rights
//  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  * copies of the Software, and to permit persons to whom the Software is
//  * furnished to do so, subject to the following conditions:
//  * 
//  * The above copyright notice and this permission notice shall be included in
//  * all copies or substantial portions of the Software.
//  * 
//  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  * THE SOFTWARE.
// </original_license>
// <version>
// **************[ starLiGHT.Engine SVN ]**********************
// * $Rev:: 620                   $:  Revision of last commit *
// * $Author:: unknown            $:  Author of last commit   *
// * $Date:: 2010-12-07 15:23:58 #$:  Date of last commit     *
// ************************************************************
// </version>
//--------------------------------------------------------------------------------
#endregion

namespace starLiGHT.ParticleSystem.Initializer
{
    #region Using Statements
    using System;
    using System.Collections.Generic;
    using System.Text;
    using starLiGHT.ParticleSystem.Common;

    #endregion

    /// <summary>
    /// The Lifetime Initializer sets a lifetime for the particle. It is usually combined with the
    /// Age modifier to age the particle over its lifetime and destroy the particle at the end of its 
    /// lifetime.
    /// </summary>
    public class Lifetime : InitializerBase
    {
        #region Member Variables
        private float _max;
        private float _min;

        #endregion

        #region Constructors
        /// <summary>
        /// The constructor creates a Lifetime Initializer to be used by an emitter.
        /// </summary>
        public Lifetime()
            : this(float.MaxValue, float.NaN)
        {
        }

        /// <summary>
        /// The constructor creates a Lifetime Initializer to be used by an emitter.
        /// <para>
        /// The lifetime of particles initialized by this call will be a random value between 
        /// the minimum and maximum values set.
        /// </para>
        /// </summary>
        /// <param name="minLifetime">
        /// The minimum lifetime for particles initialized by the instance.
        /// </param>
        /// <param name="maxLifetime">
        /// The maximum lifetime for particles initialized by the instance.
        /// </param>
        public Lifetime(float minLifetime, float maxLifetime)
        {
            _max = maxLifetime;
            _min = minLifetime;
        }

        /// <summary>
        /// The constructor creates a Lifetime Initializer to be used by an emitter.
        /// </summary>
        /// <param name="lifetime">
        /// The lifetime for particles initialized by the instance.
        /// </param>
        public Lifetime(float lifetime)
        {
            _min = _max = lifetime;
        }

        #endregion

        /// <summary>
        /// The minimum lifetime for particles initialized by the instance.
        /// </summary>
        public float MinLifetime
        {
            get { return _min; }
            set { _min = value; }
        }

        /// <summary>
        /// The maximum lifetime for particles initialized by the instance.
        /// </summary>
        public float MaxLifetime
        {
            get { return _max; }
            set { _max = value; }
        }

        /// <summary>
        /// Returns the average of MinLifetime and MaxLifetime. When setting both 
        /// values are set to the same lifetime value.
        /// </summary>
        public float LifetimeAverage
        {
            get { return _min == _max ? _min : (_max + _min) * 0.5f; }
            set { _max = _min = value; }
        }

        /// <summary>
        /// The initialize method is used by the emitter to apply the initialization
        /// to every particle. It is the key feature of the initializers and is
        /// used to initialize the state of every particle. This method 
        /// is called within the emitter's createParticle met6hod for every particle 
        /// and need not be called by the user.
        /// </summary>
        /// <param name="emitter">The Emitter that created the particle.</param>
        /// <param name="particle">The particle to be initialized.</param>
        /// <remarks>This is an internal method that is called by the emitter.</remarks>
        public override void initialize(Emitter emitter, ref Particle particle)
        {
            if (float.IsNaN(_max) || _min == _max)
            {
                particle.lifetime = _min;
            }
            else
            {
                particle.lifetime = emitter.Random.NextFloat(_min, _max); // (float)(_min + emitter.Random.NextDouble() * (_max - _min));
            }
        }
    }
}
